#include "StdAfx.h"
#include <assert.h>
#include "uiimginterface.h"
#include "UIImgGdiplus.h"
#include "UIRect.h"

CUIImgGdiplus::CUIImgGdiplus( void ): m_bLoad( FALSE ), m_iLeft( 0 ), m_iTop( 0 ), m_iWidth( 0 ), m_iHeight( 0 ), m_pImage( NULL )
    , m_iSubImgCount( 0 ), m_iImgLeft( 0 ), m_iImgTop( 0 ), m_iImgWidth( 0 ), m_iImgHeight( 0 ), m_iCurStatus( IMG_NORMAL )
{
    ZeroMemory( m_strImg, MAX_PATH );
}


CUIImgGdiplus::~CUIImgGdiplus( void )
{
    Destroy();

}

void CUIImgGdiplus::Draw( HDC hDC )
{
    UINT iImgStatus = m_iCurStatus;
    assert( iImgStatus >= 0 && iImgStatus <= m_iSubImgCount );

    if ( IMG_HOR == m_imgDirection )
    {
        int left = iImgStatus * m_iImgWidth;
        int top = 0;

        if ( NULL != m_pImage )
        {
            Graphics g( hDC );
            g.SetInterpolationMode( Gdiplus::InterpolationModeDefault );
            g.DrawImage( m_pImage, Gdiplus::Rect( m_iLeft, m_iTop, m_iWidth, m_iHeight ), left, top, m_iImgWidth, m_iImgHeight, Gdiplus::UnitPixel );
        }
    }
    else if ( IMG_VEC == m_imgDirection )
    {
        int left = 0;
        int top = iImgStatus * m_iImgHeight;

        if ( NULL != m_pImage )
        {
            Graphics g( hDC );
            g.SetInterpolationMode( Gdiplus::InterpolationModeDefault );
            g.DrawImage( m_pImage, Gdiplus::Rect( m_iLeft, m_iTop, m_iWidth, m_iHeight ), left, top, m_iImgWidth, m_iImgHeight, Gdiplus::UnitPixel );
        }
    }
}

void CUIImgGdiplus::SetMargin( const RECT& rcMargin )
{

}

BOOL CUIImgGdiplus::LoadImg( LPCTSTR lpszImg )
{
    m_iImgWidth = GetImage( lpszImg )->GetWidth();
    m_iImgHeight = GetImage( lpszImg )->GetHeight();
    m_iWidth = m_iImgWidth;
    m_iHeight = m_iImgHeight;

    return ( NULL != m_pImage );
}

BOOL CUIImgGdiplus::LoadImg( UINT uID, IMGDIRECTION imgDir, UINT iImgCount )
{
    assert( imgDir >= IMG_HOR && imgDir <= IMG_VEC );


    HRSRC   hRsrc   =   ::FindResource( g_hDllModule, MAKEINTRESOURCE( uID ), _T( "PNG" ) ); //   type

    if ( !hRsrc )
        return   FALSE;

    //   load   resource   into   memory
    DWORD   len   =   SizeofResource( g_hDllModule,   hRsrc );
    lpRsrc   = ( BYTE* )LoadResource( g_hDllModule,   hRsrc );

    if ( !lpRsrc )
        return   FALSE;

    //   Allocate   global   memory   on   which   to   create   stream
    m_hMem   =   GlobalAlloc( GMEM_FIXED,   len );
    BYTE*   pmem   = ( BYTE* )GlobalLock( m_hMem );
    memcpy( pmem, lpRsrc, len );
    CreateStreamOnHGlobal( m_hMem, FALSE, &pstm );
    m_pImage = Gdiplus::Image::FromStream( pstm );

    m_iImgWidth = m_pImage->GetWidth();
    m_iImgHeight = m_pImage->GetHeight();

    if ( IMG_VEC == imgDir )
    {
        m_iImgHeight = m_iImgHeight / iImgCount;
    }
    else if ( IMG_HOR == imgDir )
    {
        m_iImgWidth = m_iImgWidth / iImgCount;
    }

    m_iWidth = m_iImgWidth;
    m_iHeight = m_iImgHeight;

    m_iSubImgCount = iImgCount;
    m_imgDirection = imgDir;
    return ( NULL != m_pImage );
}

BOOL CUIImgGdiplus::LoadImg( LPCTSTR lpszImg, IMGDIRECTION imgDir, UINT iImgCount )
{
    assert( imgDir >= IMG_HOR && imgDir <= IMG_VEC );
    m_pImage = GetImage( lpszImg );

    if ( NULL == m_pImage )
    {
        return FALSE;
    }

    m_iImgWidth = m_pImage->GetWidth();
    m_iImgHeight = m_pImage->GetHeight();

    if ( IMG_VEC == imgDir )
    {
        m_iImgHeight = m_iImgHeight / iImgCount;
    }
    else if ( IMG_HOR == imgDir )
    {
        m_iImgWidth = m_iImgWidth / iImgCount;
    }

    m_iWidth = m_iImgWidth;
    m_iHeight = m_iImgHeight;

    m_iSubImgCount = iImgCount;
    m_imgDirection = imgDir;
    return TRUE;
}

void CUIImgGdiplus::SetPosition( int iLeft, int iTop )
{
    m_iLeft = iLeft;
    m_iTop = iTop;
}

void CUIImgGdiplus::SetSize( int iWidth, int iHeight )
{
    m_iWidth = iWidth;
    m_iHeight = iHeight;
}

Image* CUIImgGdiplus::GetImage( LPCTSTR lpszImg )
{
    if ( NULL == m_pImage )
    {
        m_pImage = new Image( lpszImg );
    }

    return m_pImage;
}

void CUIImgGdiplus::Destroy()
{
    GlobalUnlock( m_hMem );
    pstm-> Release();
    FreeResource( lpRsrc );

    if ( NULL != m_pImage )
    {
        delete m_pImage;
        m_pImage = NULL;
    }
}

const POINT CUIImgGdiplus::GetPosition()const
{
    POINT pt;
    pt.x = m_iLeft;
    pt.y = m_iTop;
    return pt;
}

const SIZE CUIImgGdiplus::GetSize()const
{
    SIZE size;
    size.cx = m_iWidth;
    size.cy = m_iHeight;
    return size;
}

void CUIImgGdiplus::GetBorder( RECT& rcBorder )
{
    ::SetRect( &rcBorder, m_iLeft, m_iTop, m_iLeft + m_iWidth, m_iTop + m_iHeight );
}

int CUIImgGdiplus::GetCurStatus()const
{
    return m_iCurStatus;
}

void CUIImgGdiplus::SetCurStatus( int iStatus )
{
    m_iCurStatus = iStatus;

    if ( m_iSubImgCount <= m_iCurStatus )
    {
        m_iCurStatus = NC_ACTIVE;
    }
}
